import requests

def fetch_weather_data(city_code):
    url = f"http://t.weather.itboy.net/api/weather/city/{city_code}"
    try:
        response = requests.get(url)
        if response.status_code != 200:
            raise Exception(f"HTTP error! status: {response.status_code}")
        return response.json()
    except Exception as error:
        print('Could not fetch weather data:', error)
        return None

def print_weather_forecast(city_code):
    weather_data = fetch_weather_data(city_code)
    if not weather_data:
        print("No weather data available.")
        return

    city_info = weather_data['cityInfo']
    city = city_info['city']
    print(f"天气预报城市：{city}\n更新时间：{city_info['updateTime']}")

    data = weather_data['data']
    print(f"当前温度：{data['wendu']}℃，湿度：{data['shidu']}，空气质量：{data['quality']}。{data['ganmao']}\n")

    forecast = data['forecast']
    print("未来几天的天气预报：")
    for day in forecast:
        print(f"{day['date']}日({day['week']}), {day['ymd']}")
        print(f"    {day['type']}, {day['low']}到{day['high']}, 风向：{day['fx']} {day['fl']}, 空气质量指数：{day['aqi']}")
        print(f"    {day['notice']}\n")

    yesterday = data['yesterday']
    print("昨日天气：")
    print(f"{yesterday['date']}日({yesterday['week']}), {yesterday['ymd']}")
    print(f"    {yesterday['type']}, {yesterday['low']}到{yesterday['high']}, 风向：{yesterday['fx']} {yesterday['fl']}, 空气质量指数：{yesterday['aqi']}")
    print(f"    {yesterday['notice']}\n")

if __name__ == '__main__':
    city_code = '101180101'  # 郑州市的城市代码
    print_weather_forecast(city_code)
